package com.cashbook.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cashbook.pojo.TbAdmin;
import com.cashbook.service.AdminService;

import entity.PageResult;
import entity.Result;
/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	
	
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbAdmin> findAll(){			
		return adminService.findAll();
	}
	
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){			
		return adminService.findPage(page, rows);
	}
	
	/**
	 * 增加
	 * @param admin
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody TbAdmin admin){
		try {
			adminService.add(admin);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param admin
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody TbAdmin admin){
		try {
			adminService.update(admin);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public TbAdmin findOne(Long id){
		return adminService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long [] ids){
		try {
			adminService.delete(ids);
			return new Result(true, "删除成功"); 
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
		/**
	 * 查询+分页
	 * @param brand
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbAdmin admin, int page, int rows  ){
		return adminService.findPage(admin, page, rows);		
	}
	
	/**
	 * 查询+分页
	 * @param brand
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/login")
	public Result search(HttpServletRequest request,HttpServletResponse response  ){
		//Long.parseLong("String")
		TbAdmin tbAdmin =new TbAdmin();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		tbAdmin.setUsername(username);
		tbAdmin.setPassword(password);
		List<TbAdmin> ls=adminService.login(tbAdmin);	
		if(ls!=null && !ls.isEmpty() ) {
			//进行拦截器出来
			HttpSession session = request.getSession();
			session.setAttribute("Global.USER_SESSION_KEY", tbAdmin);
			TbAdmin attribute = (TbAdmin)session.getAttribute("Global.USER_SESSION_KEY");
			//request.getSession().setAttribute(Global.USER_SESSION_KEY, tbAdmin);
			//return "redirect:/admin/index.html";
			return new Result(true, "登录成功"); 
		}else {
			return new Result(true, "登录失败");
			//return "redirect:/admin/login.html";
		}
			
	}
	
	@RequestMapping("/findLocalUser")
	public Result findLocalUser(HttpServletRequest request,HttpServletResponse response  ){
		HttpSession session = request.getSession();
		TbAdmin attribute =(TbAdmin)session.getAttribute("Global.USER_SESSION_KEY");
		if(attribute==null) {
			return new Result(true, "登录失败....");
		}else {
			return new Result(true, "登录成功1");
			//return "redirect:/admin/login.html";
		}
			
	}

	@RequestMapping("/findLocalUser1")
	public Result findLocalUser(HttpServletRequest request,HttpServletResponse response  ){
		HttpSession session = request.getSession();
		TbAdmin attribute =(TbAdmin)session.getAttribute("Global.USER_SESSION_KEY");
		if(attribute==null) {
			return new Result(true, "登录失败溜了溜了");
		}else {
			return new Result(true, "登录成功");
			//return "redirect:/admin/login.html";
		}

	}

	@RequestMapping("/findLocalUser111")
	public Result findLocalUser(HttpServletRequest request,HttpServletResponse response  ){
		System.out.println("ok你代的");
		System.out.println("ok你代扣卡代扣的");
		System.out.println("ok你代扣卡代扣的");
		System.out.println("哈哈");
		System.out.println("用的就封建时代");
		System.out.println("用的就封建时代111111");
		System.out.println("ok你代的");
		System.out.println("打得动");
		System.out.println("答复");
		System.out.println("的范围范围");
	}
	
}
